﻿using Newtonsoft.Json;
using pTools.ChildWin;
using pTools.cMethods;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using pTools.ProductPlanPrint;
using pTools.TuQiangPrint;

namespace pTools
{
    public partial class loadingWin : Form
    {
        DBHelper db;
        TxtLogHelper txt;
        public loadingWin()
        {
            InitializeComponent();
            db = new DBHelper();
            txt = new TxtLogHelper();
        }

        private void loadingWin_Load(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> par = new Dictionary<string, string>();
                var table = getSSoInfo();
                if (table.Rows.Count > 0)
                {
                    txt.AddLogMsg("信息存在，开始执行单点方法");
                    string targetWin = table.Rows[0]["targetWin"].ToString();
                    CommonMethods.userCode = table.Rows[0]["loginUser"].ToString();
                    CommonMethods.userName = table.Rows[0]["userName"].ToString();
                    CommonMethods.baseCode = table.Rows[0]["baseCode"].ToString();
                    string sql = "update sys_ClientSSo_info set status=0 where" +
                                " loginUser='" + CommonMethods.userCode + "'";

                    // 生产打码 /期初打码
                    if (targetWin.Contains("production-bar-print") )
                    {
                        ProductionPrint pp = new ProductionPrint();
                        pp.ShowDialog();
                        this.Close();
                    }
                    else if (targetWin.Contains("reBar"))
                    {
                        ReBarWin win = new ReBarWin(table.Rows[0]["dataId"].ToString());
                        win.ShowDialog();
                        this.Close();
                    }
                    else if (targetWin.Contains("fj-win"))
                    {
                        string dataId = "";
                        if (table.Rows[0]["dataId"].ToString() != "0")
                        {
                            dataId = table.Rows[0]["dataId"].ToString();
                        }
                        FuJuan win = new FuJuan(dataId);
                        win.ShowDialog();
                        this.Close();
                    }
                }
                label1.Text = "程序跳转异常，请速速与开发人员联系！！";
                this.Text = "插件跳转过度界面";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// 查询单点信息
        /// </summary>
        /// <returns></returns>
        private DataTable getSSoInfo()
        {
            try
            {
                txt.AddLogMsg("开始获取单点数据");
                string[] args = Environment.GetCommandLineArgs();
                string ssoId = CleanInput(args[1]);
                //string ssoId = "4f0777da657d4c01b831c78ceaa08938";
                var sql = "select * from sys_ClientSSo_info where id='" + ssoId + "' order by Id desc";
                var table = db.getData(sql, "");
                txt.AddLogMsg("数据获取条数:" + table.Rows.Count);
                return table;
            }
            catch (Exception ex)
            {
                txt.AddLogMsg("getSSoInfo方法程序异常：" + ex.Message);
            }
            return null;
        }

        private string CleanInput(string input)
        {
            return input.Substring(16).TrimEnd('/');
            //return input;
        }

    }
}
